/**
 *
 */
package com.wedroid.r2d2.contact;

import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;

import com.alibaba.fastjson.annotation.JSONField;
import com.wedroid.entity.contact.ContacterType;
import com.wedroid.r2d2.message.RecommendInfo;

/**
 * @author lute
 *
 */
public class Contact {

    private static final String[] SPECIAL_CONTACTS = {
            "newsapp", "fmessage", "filehelper", "weibo", "qqmail", "tmessage", "qmessage",
            "qqsync", "floatbottle", "lbsapp", "shakeapp", "medianote", "qqfriend", "readerapp",
            "blogapp", "facebookapp", "masssendapp", "meishiapp", "feedsapp", "voip", "blogappweixin",
            "weixin", "brandsessionholder", "weixinreminder", "wxid_novlwrv3lqwv11", "gh_22b87fa7cb3c",
            "officialaccounts", "notification_messages", "wxid_novlwrv3lqwv11", "gh_22b87fa7cb3c",
            "wxitil", "userexperience_alarm", "notification_messages"
    };

    @JSONField(name = "Uin")
    private long uin;

    @JSONField(name = "UserName")
    private String userName;

    @JSONField(name = "NickName")
    private String nickName;

    @JSONField(name = "HeadImgUrl")
    private String headImgUrl;

    @JSONField(name = "ContactFlag")
    private int contactFlag;

    @JSONField(name = "MemberCount")
    private int memberCount;

    @JSONField(name = "RemarkName")
    private String remarkName;

    @JSONField(name = "Sex")
    private int sex;

    @JSONField(name = "VerifyFlag")
    private int verifyFlag;

    @JSONField(name = "Province")
    private String province;

    @JSONField(name = "City")
    private String city;

    @JSONField(name = "Alias")
    private String alias;

    @JSONField(name = "SnsFlag")
    private int snsFlag;

    @JSONField(name = "DisplayName")
    private String displayName;

    @JSONField(name = "ChatRoomId")
    private int chatRoomId;

    @JSONField(name = "KeyWord")
    private String keyWord;

    @JSONField(name = "EncryChatRoomId")
    private String encryChatRoomId;

    @JSONField(name = "Signature")
    private String signature;

    /**
     *
     */
    public Contact() {

    }

    /**
     * @param recommendInfo
     */
    public Contact(RecommendInfo recommendInfo) {
        this.userName = recommendInfo.getUserName();
        this.nickName = recommendInfo.getNickName();
        this.province = recommendInfo.getProvince();
        this.city = recommendInfo.getCity();
        this.alias = recommendInfo.getAlias();
        this.sex = recommendInfo.getSex();
    }

    /**
     * Determine the contact is a official account
     * @return
     */
    public boolean isOfficial() {
        return (verifyFlag & 8) != 0;
    }

    /**
     * Determine the account is a group chat
     * @return
     */
    public boolean isGroup() {
        return userName.indexOf("@@") != -1;
    }

    /**
     * Determine whether the given user name of the contact is special
     * @return
     */
    public boolean isSpecial() {
        return ArrayUtils.contains(SPECIAL_CONTACTS, userName);
    }

    /**
     * @return
     */
    public boolean isSelf() {
        return uin > 0;
    }

    /**
     * @return
     */
    public ContacterType getContacterType() {
        if (isOfficial()) {
            return ContacterType.OFFICIAL;
        } else if (isGroup()) {
            return ContacterType.GROUP;
        } else if (isSpecial()) {
            return ContacterType.SPECIAL;
        } else {
            return ContacterType.FRIEND;
        }
    }

    /**
     * Get account preferred name
     * @return
     */
    public String getPreferredName() {
        if (StringUtils.isNotBlank(remarkName)) {
            return remarkName;
        } else if (StringUtils.isNotBlank(displayName)) {
            return displayName;
        } else if (StringUtils.isNotBlank(nickName)) {
            return nickName;
        }
        return null;
    }

    /* (non-Javadoc)
     * @see java.lang.Object#equals(java.lang.Object)
     */
    @Override
    public boolean equals(Object obj) {
        if (obj == this) {
            return true;
        }
        if (!(obj instanceof Contact)) {
            return false;
        }
        Contact contact = (Contact) obj;
        return contact.getUserName().equals(userName);
    }

    /* (non-Javadoc)
     * @see java.lang.Object#hashCode()
     */
    @Override
    public int hashCode() {
        int result = 17;
        result = 31 * result + userName.hashCode();
        return result;
    }

    /**
     * @return the uin
     */
    public long getUin() {
        return uin;
    }

    /**
     * @return the userName
     */
    public String getUserName() {
        return userName;
    }

    /**
     * @return the nickName
     */
    public String getNickName() {
        return nickName;
    }

    /**
     * @return the headImgUrl
     */
    public String getHeadImgUrl() {
        return headImgUrl;
    }

    /**
     * @return the contactFlag
     */
    public int getContactFlag() {
        return contactFlag;
    }

    /**
     * @return the memberCount
     */
    public int getMemberCount() {
        return memberCount;
    }

    /**
     * @return the remarkName
     */
    public String getRemarkName() {
        return remarkName;
    }

    /**
     * @return the sex
     */
    public int getSex() {
        return sex;
    }

    /**
     * @return the verifyFlag
     */
    public int getVerifyFlag() {
        return verifyFlag;
    }

    /**
     * @return the province
     */
    public String getProvince() {
        return province;
    }

    /**
     * @return the city
     */
    public String getCity() {
        return city;
    }

    /**
     * @return the alias
     */
    public String getAlias() {
        return alias;
    }

    /**
     * @return the snsFlag
     */
    public int getSnsFlag() {
        return snsFlag;
    }

    /**
     * @return the displayName
     */
    public String getDisplayName() {
        return displayName;
    }

    /**
     * @return the chatRoomId
     */
    public int getChatRoomId() {
        return chatRoomId;
    }

    /**
     * @return the keyWord
     */
    public String getKeyWord() {
        return keyWord;
    }

    /**
     * @return the encryChatRoomId
     */
    public String getEncryChatRoomId() {
        return encryChatRoomId;
    }

    /**
     * @return the signature
     */
    public String getSignature() {
        return signature;
    }

    /**
     * @param uin the uin to set
     */
    public void setUin(long uin) {
        this.uin = uin;
    }

    /**
     * @param userName the userName to set
     */
    public void setUserName(String userName) {
        this.userName = userName;
    }

    /**
     * @param nickName the nickName to set
     */
    public void setNickName(String nickName) {
        this.nickName = nickName;
    }

    /**
     * @param headImgUrl the headImgUrl to set
     */
    public void setHeadImgUrl(String headImgUrl) {
        this.headImgUrl = headImgUrl;
    }

    /**
     * @param contactFlag the contactFlag to set
     */
    public void setContactFlag(int contactFlag) {
        this.contactFlag = contactFlag;
    }

    /**
     * @param memberCount the memberCount to set
     */
    public void setMemberCount(int memberCount) {
        this.memberCount = memberCount;
    }

    /**
     * @param remarkName the remarkName to set
     */
    public void setRemarkName(String remarkName) {
        this.remarkName = remarkName;
    }

    /**
     * @param sex the sex to set
     */
    public void setSex(int sex) {
        this.sex = sex;
    }

    /**
     * @param verifyFlag the verifyFlag to set
     */
    public void setVerifyFlag(int verifyFlag) {
        this.verifyFlag = verifyFlag;
    }

    /**
     * @param province the province to set
     */
    public void setProvince(String province) {
        this.province = province;
    }

    /**
     * @param city the city to set
     */
    public void setCity(String city) {
        this.city = city;
    }

    /**
     * @param alias the alias to set
     */
    public void setAlias(String alias) {
        this.alias = alias;
    }

    /**
     * @param snsFlag the snsFlag to set
     */
    public void setSnsFlag(int snsFlag) {
        this.snsFlag = snsFlag;
    }

    /**
     * @param displayName the displayName to set
     */
    public void setDisplayName(String displayName) {
        this.displayName = displayName;
    }

    /**
     * @param chatRoomId the chatRoomId to set
     */
    public void setChatRoomId(int chatRoomId) {
        this.chatRoomId = chatRoomId;
    }

    /**
     * @param keyWord the keyWord to set
     */
    public void setKeyWord(String keyWord) {
        this.keyWord = keyWord;
    }

    /**
     * @param encryChatRoomId the encryChatRoomId to set
     */
    public void setEncryChatRoomId(String encryChatRoomId) {
        this.encryChatRoomId = encryChatRoomId;
    }

    /**
     * @param signature the signature to set
     */
    public void setSignature(String signature) {
        this.signature = signature;
    }

}
